<?php

class mvytvor{
	var $kto;
    public function __construct($kto) {
        if ($kto == "sutazna_kategoria" or $kto == "typ_kategoria_hodnotenia" or $kto == "sutaziaci" or $kto == "sutaz" or $kto == "sutazne_kolo") {
            $this->kto = $kto;
        } else {
            throw new Exception("vytvor: zly vstup na vytvorenie db");
        }
    }
	
	public function vytvor($meno, $popis) {
		if ($this->kto=='sutaz'){
			$query_zapis_reg = "INSERT INTO ".$this->kto." (Meno, Popis, idOrganizator) VALUES ('".mysql_real_escape_string($meno)."', '".mysql_real_escape_string( $popis)."', '".$_SESSION['user']->id."') ";
		} else {
			if ($this->kto=='sutazne_kolo'){
				$query_zapis_reg = "INSERT INTO ".$this->kto." (meno, popis, id_sutaz, id_sutazna_kategoria) VALUES ('".mysql_real_escape_string($meno)."', '".mysql_real_escape_string( $popis)."', '".$_SESSION['idupravsu']."', '".$_SESSION['vytvorkolo']."') ";
			} else {
			$query_zapis_reg = "INSERT INTO ".$this->kto." (Meno, Popis) VALUES ('".mysql_real_escape_string($meno)."', '".mysql_real_escape_string($popis)."') ";
			}
		}
		conndb::getInst()->query($query_zapis_reg);		
	}
	
	public function kotrola($meno){
		conndb::getInst()->query("SELECT * FROM  ".$this->kto." WHERE Meno=\"$meno\"");
		$possible = conndb::getInst()->getRowAsObject();	
		if($possible == "") {return true;} else {return false;}
	}
	
	public function nacitaj($iduprav){
		conndb::getInst()->query("SELECT * FROM ".$this->kto." WHERE id=\"$iduprav\" ");
		return conndb::getInst()->getRowAsObject();
	}
	
	public function poslednasutaz(){
		conndb::getInst()->query("SELECT id FROM sutaz WHERE idOrganizator='".$_SESSION['user']->id."' ORDER BY ID DESC");
		return conndb::getInst()->getRowAsObject();
	}
	
	public function kotrolauprav($iduprav){
		switch($this->kto){
			case "sutazna_kategoria":
				conndb::getInst()->query("SELECT c.body FROM sutazne_kolo a INNER JOIN sutaziaci_v_kole b on (b.id_sutazne_kolo=a.id) 
										INNER JOIN hodnotenie c on (c.id_sutaziaci_v_kole=b.id) WHERE a.id_sutazna_kategoria='".$iduprav."' ");
				break;
			case "sutaziaci":
				conndb::getInst()->query("SELECT c.body FROM sutaziaci_v_kole a INNER JOIN hodnotenie c on (c.id_sutaziaci_v_kole=a.id) WHERE a.id_sutaziaci='".$iduprav."' ");
				break;
			case "typ_kategoria_hodnotenia":
				conndb::getInst()->query("SELECT c.body FROM typ_kategoria_hodnotenia a INNER JOIN hodnotenie c on (c.id_kategoria_hodnotenia=a.id) WHERE a.id='".$iduprav."' ");
				break;
			case "sutaz":
				conndb::getInst()->query("SELECT c.body FROM sutazne_kolo a INNER JOIN sutaziaci_v_kole b on (b.id_sutazne_kolo=a.id) 
									INNER JOIN hodnotenie c on (c.id_sutaziaci_v_kole=b.id) WHERE a.id_sutaz='".$iduprav."' ");
				break;
			case "sutazne_kolo":
				conndb::getInst()->query("SELECT c.body FROM sutazne_kolo a INNER JOIN sutaziaci_v_kole b on (b.id_sutazne_kolo=a.id) 
									INNER JOIN hodnotenie c on (c.id_sutaziaci_v_kole=b.id) WHERE a.id='".$iduprav."' ");
				break;
		}
								
		$possible = conndb::getInst()->getRowAsObject();	
		if($possible == "") {return true;} else {return false;}
	}
	
	public function uprav($meno, $popis, $id){
		conndb::getInst()->query("UPDATE ".$this->kto." SET meno=\"".$meno."\", popis=\"".$popis."\" WHERE id=\"".$id."\"");
	}
}
?>